package jhavelogger.data;
import com.maxmind.geoip.*;
import java.io.IOException;

/**
 * Uses the GeoIP database to provide location information for a specified
 * IP address. This file requires the GeoLitecity.dat file (currently located
 * at the root of the project).
 * 
 * @author Roman Bykhovoy
 */
public class GeoIP implements IPLookup {
    /**
     * Returns location information for the specified ip address.
     * Any unknown fields will be populated with nulls
     * 
     * @param ip the ip address to lookup
     * @return GeoIP location object
     */
    @Override
    public Location getIpLocation(String ip){
	LookupService service = null;
	
	try{
	    service = new LookupService("./GeoLiteCity.dat",LookupService.GEOIP_MEMORY_CACHE );
	    Location location = service.getLocation(ip);
	    return location;
	}
	catch (IOException e) {
	    e.printStackTrace();
	} finally {
	    service.close();
	}
	
	return null;
    }
}
